#define _CRT_SECURE_NO_WARNINGS

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        vector<int> ret = nums;
        int left = 0;
        int right = nums.size() - 1;
        int end = nums.size();
        while (left <= right)
        {
            if (abs(nums[left]) > abs(nums[right]))
            {
                ret[--end] = nums[left] * nums[left];
                left++;
            }
            else
            {
                ret[--end] = nums[right] * nums[right];
                right--;
            }
        }
        return ret;
    }
};

//class Solution {
//public:
//    int minSubArrayLen(int target, vector<int>& nums) {
//        int sum = 0;
//        int ret = nums.size() + 1;
//        for (int i = 0; i < nums.size(); i++)
//        {
//            for (int j = i; j < nums.size(); j++)
//            {
//                sum = 0;
//                for (int k = i; k <= j; k++)
//                {
//                    sum += nums[k];
//                }
//                if (sum >= target && ret > (j - i + 1))
//                {
//                    ret = j - i + 1;
//                }
//            }
//        }
//        if (ret == (nums.size() + 1))
//            return 0;
//        return ret;
//    }
//};

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum = 0;
        int ret = nums.size() + 1;
        int i = 0;
        for (int j = 0; j < nums.size(); j++)
        {
            sum += nums[j];
            while (sum >= target)
            {
                if (ret > (j - i + 1))
                {
                    ret = (j - i + 1);
                }
                sum -= nums[i++];
            }
        }
        if (ret == (nums.size() + 1))
            return 0;
        return ret;
    }
};


class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int i = 0;
        int j = n - 1;
        int k = 0;
        int num = 0;
        vector<vector<int>> ret(n, vector<int>(n, 0));
        while (i <= j)
        {
            k = i;
            while (k <= j - 1)
                ret[i][k++] = (++num);
            k = i;
            while (k <= j - 1)
                ret[k++][j] = (++num);
            k = j;
            while (k >= i + 1)
                ret[j][k--] = (++num);
            k = j;
            while (k >= i + 1)
                ret[k--][i] = (++num);
            i++;
            j--;
        }
        if (n % 2 == 1)
        {
            ret[(n - 1) / 2][(n - 1) / 2] = (++num);
        }
        return ret;
    }
};